<extend name="Public/base" />
<block name="main">
    <script type="text/javascript" src="__PUBLIC__/dataTable/jquery.dataTables.min.js"></script>
    <link rel="stylesheet" href="__PUBLIC__/css/dataTable.css">
    <fieldset class="layui-elem-field">
        <legend>日志列表</legend>
        <div class="layui-field-box">
            <form class="layui-form" id="form-admin-add" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <input type="text" name="userName" placeholder="姓名" class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <input type="text" name="userPhone" placeholder="手机号" class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <select name="isPay" lay-verify="">
                            <option value="">是否付款</option>
                            <volist name="isPay" id="vo">
                                <option value="{$key}">{$vo}</option>
                            </volist>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <select name="batch" lay-verify="">
                            <option value="">批次</option>
                            <volist name="batch" id="vo">
                                <option value="{$key}">{$vo}</option>
                            </volist>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <select name="agent" lay-verify="">
                            <option value="">代理</option>
                            <volist name="agent" id="vo">
                                <option value="{$key}">{$vo}</option>
                            </volist>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <select name="isSign" lay-verify="">
                            <option value="">状态</option>
                            <volist name="isSign" id="vo">
                                <option value="{$key}">{$vo}</option>
                            </volist>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <span class="layui-btn sub">查询</span>
                        <span class="layui-btn layui-btn-normal api-add"><i class="layui-icon">&#xe608;</i> 新增</span>
                        <span class="layui-btn layui-btn-normal api-import"><i class="layui-icon">&#xe609;</i> 导出</span>
                        <span class="layui-btn layui-btn-normal api-count"><i class="layui-icon">&#xe659;</i> 计算</span>
                    </div>
                </div>
            </form>
            <table class="layui-table" id="list-admin" lay-even>
                <thead>
                <tr>
                    <th>姓名</th>
                    <th>手机号</th>
                    <th>地址</th>
                    <th>类型/数量</th>
                    <th>单价</th>
                    <th>代理</th>
                    <th>快递单号</th>
                    <th>订单状态</th>
                    <th>批次</th>
                    <th>描述</th>
                    <th>操作</th>
                </tr>
                </thead>
            </table>
        </div>
    </fieldset>
</block>
<block name="myScript">
    <script>
        /**
         * 格式化时间戳
         * @param fmt
         * @returns {*}
         * @constructor
         */
        Date.prototype.Format = function (fmt) {
            var o = {
                "M+": this.getMonth() + 1, //月份
                "d+": this.getDate(), //日
                "h+": this.getHours(), //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        };

        layui.use(['layer', 'form'], function() {
            $(document).on('click', '.confirm', function () {
                var ownObj = $(this);
                layer.confirm(ownObj.attr('data-info'), {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    $.ajax({
                        type: "POST",
                        url: ownObj.attr('data-url'),
                        data: {id:ownObj.attr('data-id')},
                        success: function(msg){
                            if( msg.code == 1 ){
                                location.reload();
                            }else{
                                layer.msg(msg.msg, {
                                    icon: 5,
                                    shade: [0.6, '#393D49'],
                                    time:1500
                                });
                            }
                        }
                    });
                });
            });


            $('.api-add').on('click', function () {
                layer.open({
                    type: 2,
                    area: ['90%', '90%'],
                    maxmin: true,
                    content: '{:U("add")}'
                });
            });

            $('.api-import').on('click', function () {
                layer.open({
                    type: 2,
                    area: ['60%', '60%'],
                    maxmin: true,
                    content: '{:U("import")}'
                });
            });
            $('.api-count').on('click', function () {
                layer.open({
                    type: 2,
                    area: ['90%', '90%'],
                    maxmin: true,
                    content: '{:U("count")}'
                });
            });

            $(document).on('click', '.edit', function () {
                var ownObj = $(this);
                layer.open({
                    type: 2,
                    area: ['90%', '90%'],
                    maxmin: true,
                    content: ownObj.attr('data-url')+'&id='+ownObj.attr('data-id')
                });
            });

            var myFun = function (query) {
                query = query || '';
                return $('#list-admin').DataTable({
                    dom: 'rt<"bottom"ifpl><"clear">',
                    ordering: false,
                    autoWidth: false,
                    searching:false,
                    serverSide: true,
                    ajax: {
                        url:'{:U("ajaxGetIndex")}' + encodeURI(query),
                        type: 'POST',
                        dataSrc: function ( json ) {
                            if( json.code == 0 ){
                                parent.layer.msg(json.msg, {
                                    icon: 5,
                                    shade: [0.6, '#393D49'],
                                    time:1500
                                });
                            }else{
                                return json.data;
                            }
                        }
                    },
                    columnDefs:[
                        {
                            "targets":2,
                            "render":function(data, type, row){
                                return row.province + "-" + row.city + "-" + row.district + '-' + data;
                            }
                        },
                        {
                            "targets":3,
                            "render":function(data,type, row){
                                if(data == 1) {
                                    return '普通['+ row.buyNumber+'箱]';
                                }
                                if(data == 2){
                                    return '精品['+ row.buyNumber+'箱]';
                                }
                                if(data == 3){
                                    return '小果['+ row.buyNumber+'箱]';
                                }
                            }
                        },
                        {
                            "targets":4,
                            "render":function(data,type, row){
                                var pay = '';
                                if(row.isPay == 1) {
                                    pay = '已付款';
                                }
                                if(row.isPay == 2) {
                                    pay = '未付款';
                                }
                                if(row.isPay == 3) {
                                    pay = '部分付';
                                }
                                if(row.isPay == 4) {
                                    pay = '不需付';
                                }
                                return data+'['+ pay +']';
                            }
                        },
                        {
                            "targets":6,
                            "render":function(data, type, row){

                                if(!data && typeof(data)!="undefined" && data!=0) {
                                    return '';
                                } else {
                                    return row.expressName + "[" + row.expressNumber + "]";
                                }

                            }
                        },
                        {
                            "targets":7,
                            "render":function(data, type, row){
                                if(!row.expressNumber && typeof(row.expressNumber)!="undefined" && row.expressNumber!=0) {
                                    return '未发货';
                                } else {
                                    if(data == 0) {
                                        return '已发货';
                                    }
                                    if(data == 2) {
                                        return '派件中';
                                    }
                                    if(data == 1) {
                                        return '已签收';
                                    }
                                }

                            }
                        },
                        {
                         "targets":10,
                         "render":function(data, type, row){
                         var returnStr = '';
                         returnStr += '<span class="layui-btn edit layui-btn-normal" ' +
                         'data-id="' + row.id +'" data-url="{:U(\'put\')}">发货</span>';
                             returnStr += '<span class="layui-btn edit layui-btn-normal" ' +
                                     'data-id="' + row.id +'" data-url="{:U(\'edit\')}">修改</span>';
                         returnStr += '<span class="layui-btn layui-btn-danger confirm" ' +
                         'data-id="' + row.id +'" data-info="你确定删除当前订单么？" data-url="{:U(\'del\')}">删除</span>';
                         return returnStr;
                         }
                         }
                    ],
                    iDisplayLength : 20,
                    aLengthMenu : [20, 30, 50],
                    columns: [
                        {"data": "userName"},
                        {"data": "userPhone"},
                        {"data": "userAddress" },
                        {"data": "buyType" },
                        {"data": "buyPrice" },
                        {"data": "agent"},
                        {"data": "expressNumber" },
                        {"data": "isSign" },
                        {"data": "batch" },
                        {"data": "remarks" },
                        {"data": null }
                    ]
                });
            };
            var myTable = myFun();
            $('.sub').on("click", function(){
                myTable.destroy();
                myTable = myFun('&'+ $('#form-admin-add').serialize());
            });
        });
    </script>
</block>